package com.cn.demo.springboot.service.user.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cn.demo.springboot.dao.UserMapper;
import com.cn.demo.springboot.entity.User;
import com.cn.demo.springboot.service.user.UserService;


@Service
@Transactional
public class UserServiceImpl implements UserService {
	private final static Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
	@Autowired
	private UserMapper userMapper;
	//@Autowired
/*	private ApplicationContext WebApplicationContext;
	*/
	@Transactional
	@Override
	public void update(User user) {
		
		userMapper.add(user);
		if(true){
			throw new RuntimeException("dhjdfgjd");
		}
	}
	
	@Override
	public void insert(User user) {
	}

	
	@Transactional
	@Override
	public List<User> get() {
/*
		Thread thread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				UserService service = WebApplicationContext.getBean(UserService.class);
				User user = new User();//.builder().address("address").birthday(new Date()).name("1123").build();
				user.setAddress("address");
				user.setBirthday(new Date());
				user.setName("111122");
				service.insert(user);
			}
		});
		thread.start();
		PageHelper.startPage(1, 2);
		User findUser = new User();
		findUser.setName("name");
		List<User> select = userMapper.select(findUser);
		log.info("select = " +select.size());
		
		List<User> selectAll = userMapper.selectAll();
		log.info("selectAll  =  "+selectAll.size());
		for (User user : selectAll) {
			log.info(user.toString());
		}
		log.info("\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n");
		for (User user : select) {
			log.info(user.toString());
		}*/
		return null;
	}

	@Override
	public User findUserByUserName(String phone) {
		User arg0 = new User();
		arg0.setName(phone);
		List<User> select = userMapper.select(arg0);
		return (select !=null && select.size()>0)?select.get(0):null; 
	}

	@Override
	public Set<String> getPermissions(String userId) {
		Set<String> strings = new HashSet<String>();
		strings.add("admin");
		return strings;
	} 
}
